package com.jsgt.greekfire.ui.home.entrance.createwallet.checkhelpword

import android.app.Activity
import android.content.Intent
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.jsgt.greekfire.R
import com.jsgt.greekfire.base.BaseBackgroundActivity
import com.jsgt.greekfire.dao.model.IdentityBean
import com.jsgt.greekfire.ui.mine.user.backupwalletword.adapter.WordHelperAdapter
import com.jsgt.greekfire.dao.viewmodel.WalletViewModel
import kotlinx.android.synthetic.main.activity_check_help_word.*

/**
 * 验证助记词
 */
class CheckHelpWordActivity : BaseBackgroundActivity() {

    private val walletViewModel: WalletViewModel by lazy {
        ViewModelProvider(this).get(WalletViewModel::class.java)
    }
    private var identityId = -1
    private var identity: IdentityBean? = null

    private var launcher: ActivityResultLauncher<Intent>? = null

    private var wordList: ArrayList<String>? = null
    private var mnemonic: String? = null

    override fun getLayoutId(): Int = R.layout.activity_check_help_word

    override fun onBackPressed() {
        super.onBackPressed()
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
    }

    override fun main() {
        //禁止截屏
//        window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
//        title = getString(R.string.check_help_word)
        identityId = intent.getIntExtra("identityId", -1)
        identity = intent.getParcelableExtra("identity")
        if (identityId == -1 && identity == null) {
            finish()
        }

        back.setOnClickListener {
            onBackPressed()
        }
        launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == Activity.RESULT_OK) {
                setResult(RESULT_OK)
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
                finish()
            }
        }

        val wordHelperAdapter = WordHelperAdapter()
        word_rv.adapter = wordHelperAdapter
        next_step_tv.setOnClickListener {
            if (identity == null) {
                launcher?.launch(
                    Intent(
                        this,
                        CheckHelpWord2Activity::class.java
                    ).putExtra("identityId", identityId)
                )
            } else {
                launcher?.launch(
                    Intent(
                        this,
                        CheckHelpWord2Activity::class.java
                    ).putExtra("identity", identity)
                )
            }
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
        }

        if (identity == null) {
            walletViewModel.getIdentity(identityId)?.observe(this, Observer {
                if (it != null && mnemonic == null) {
                    mnemonic = it.getMnemonicByAES()
                    wordList = mnemonic?.split(" ") as ArrayList<String>
                    wordHelperAdapter.addData(wordList!!)
                }
            })
        } else {
            mnemonic = identity!!.getMnemonicByAES()
            wordList = mnemonic?.split(" ") as ArrayList<String>
            wordHelperAdapter.addData(wordList!!)
        }
    }
}
